<template>

  <scroller height="80vh">
  <div class="file-list">
    <div class="file-list table" v-if="mode == ViewMode.Table" fit="true">
      <el-table :data="filteredFiles">
        <el-table-column label="名称" width="250">
          <template slot-scope="scope">
            <span v-if="scope.row.isdir">
              <ri class="dir-icon" i="folder"></ri>
              <a :href="fileURL(scope.row)"><strong>{{scope.row.name}}</strong></a>
            </span>
            <span v-else>
              <ri class="file-icon" i="file"></ri>
              <a :href="fileURL(scope.row)">{{scope.row.name}}</a>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="intro" label="简介"></el-table-column>
        <el-table-column label="修改时间" width="200">
          <template slot-scope="scope">
            <span style="margin-left: 10px"> {{ timeAgo(scope.row.modtime) }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-else class="file-list simple">
    <div v-for="(f, i) in filteredFiles" :key="i" class="file-entry" :class="f.isdir ? 'dir' : 'file'">
      <a :href="fileURL(f)"> <!-- :href="'/#/mats/file/'+dir+'/'+f.name">-->
        {{f.name}}
      </a>
    </div>
    </div>
  </div>
  </scroller>
</template>


<script>
import paths from "@js/utils/paths.js";
import Scroller from "@tools/Scroller.vue";

const ViewMode = {
  Simple: 0,
  Table: 1,
};

export default {
  components: {
    Scroller
  },
  props: ["mat", "dir", "version", "showHidden"],
  data() {
    return {
      mode: ViewMode.Table,
      ViewMode: ViewMode,
    };
  },
  created() {
  },
  computed: {
    files() {
      let files = this.mat.entries;
      files.sort((a, b) => b.isdir - a.isdir)
      return files;
    },
    filteredFiles() {
      if (this.showHidden) {
        return this.files;
      } else {
        return this.files.filter(f => !f.name.startsWith("."));
      }
    }
  },
  methods: {
    fileURL(f) {
      let p = this.$route.path;
      let nodetype = f.isdir ? "tree" : "blob";
      let v = this.version || "master";
      let middle = paths.join("/"+ nodetype, v);
      p = p.replace("/tree/"+v, middle);
      if (this.$route.params.nodetype !== undefined) {
        p = paths.join("/#/", p, f.name);
      } else {
        p = paths.join("/#/", p, middle, f.name);
      }
      return p;
    },
    timeAgo(time) {
      return window.moment(time).fromNow();
    }
  }
};
</script>

<style lang="stylus">
.file-list
  padding 1vh 1vw
  text-align left
  align-items left

  &.simple
    column-count 3

    .file-entry
      padding 5px
    .dir
      font-weight bold

  &.table
    padding 0

    .dir-icon,.file-icon
      margin-right 0.5vw
      color #409EFF

    a
      color #666

</style>